<?php
/**
 * Created by PhpStorm.
 * User: CCA
 * Date: 2019/11/11
 * Time: 13:59
 */

namespace app\api\controller;


use app\api\BaseController;
use app\common\model\Member;
use app\common\model\Voucher;

class Pointvoucher extends BaseController
{

    public function select(Voucher $voucher_model,Member $member){
        //代金券模板状态
        $templatestate_arr = $voucher_model->getTemplateState();

        //查询会员信息
        $member_info = $member->getMemberInfoByID($this->user_id);

        //查询代金券列表
        $where = array();
        $where['vouchertemplate_state'] = $templatestate_arr['usable'][0];
        $where['vouchertemplate_enddate'] = array('gt',time());
        if (intval(input('storeclass_id')) > 0){
            $where['vouchertemplate_sc_id'] = intval(input('storeclass_id'));
        }
        if (intval(input('price')) > 0){
            $where['vouchertemplate_price'] = intval(input('price'));
        }
        //查询仅我能兑换和所需积分
        $points_filter = array();
        if (intval(input('isable')) == 1){
            $points_filter['isable'] = $member_info['member_points'];
        }
        if (intval(input('points_min')) > 0){
            $points_filter['min'] = intval(input('points_min'));
        }
        if (intval(input('points_max')) > 0){
            $points_filter['max'] = intval(input('points_max'));
        }
        if (count($points_filter) > 0){
            asort($points_filter);
            if (count($points_filter) > 1){
                $points_filter = array_values($points_filter);
                $where['vouchertemplate_points'] = array('between',array($points_filter[0],$points_filter[1]));
            } else {
                if ($points_filter['min']){
                    $where['vouchertemplate_points'] = array('egt',$points_filter['min']);
                } elseif ($points_filter['max']) {
                    $where['vouchertemplate_points'] = array('elt',$points_filter['max']);
                } elseif ($points_filter['isable']) {
                    $where['vouchertemplate_points'] = array('elt',$points_filter['isable']);
                }
            }
        }
        //排序
        switch (input('orderby')){
            case 'exchangenumdesc':
                $orderby = 'vouchertemplate_giveout desc,';
                break;
            case 'exchangenumasc':
                $orderby = 'vouchertemplate_giveout asc,';
                break;
            case 'pointsdesc':
                $orderby = 'vouchertemplate_points desc,';
                break;
            case 'pointsasc':
                $orderby = 'vouchertemplate_points asc,';
                break;
            default:
                $orderby = '';
        }
        $orderby .= 'vouchertemplate_id desc';
        $voucherlist = $voucher_model->getVouchertemplateList($where, '*', 0, 18, $orderby);
        $data = array();
        $data['voucherlist'] = $voucherlist;
        $data['show_page'] = $voucher_model->page_info->render();

        //查询代金券面额
//        $pricelist = $voucher_model->getVoucherPriceList();
//        $data['pricelist'] = $pricelist;

        //查询店铺分类
        $store_class = rkcache('storeclass', true);
        $data['store_class'] = $store_class;

        return json_success_return($data);
    }

    public function ulist(Voucher $voucher,\app\common\model\Order $order){
        $voucher_list = $voucher->getMemberVoucherList($this->user_id, input('param.select_detail_state'), 10);

        //取已经使用过并且未有voucher_order_id的代金券的订单ID
        $used_voucher_code = array();
        $voucher_order = array();
        if (!empty($voucher_list)) {
            foreach ($voucher_list as $v) {
                if ($v['voucher_state'] == 2 && empty($v['voucher_order_id'])) {
                    $used_voucher_code[] = $v['voucher_code'];
                }
            }
        }
        if (!empty($used_voucher_code)) {
            $order_list = $order->getOrdercommonList(array('voucher_code'=>array('in',$used_voucher_code)),'order_id,voucher_code');
            if (!empty($order_list)) {
                foreach ($order_list as $v) {
                    $voucher_order[$v['voucher_code']] = $v['order_id'];
                    $voucher->editVoucher(array('voucher_order_id'=>$v['order_id']),array('voucher_code'=>$v['voucher_code']));
                }
            }
        }
        $data['voucher_list'] = $voucher_list;
        $data['voucherstate_arr'] = $voucher->getVoucherStateArray();
        $data['show_page'] = $voucher->page_info->render();

        json_success_return($data);
    }

    public function exchange(Voucher $voucher){
        $vid = intval(input('param.vid'));

        if ($vid <= 0){
            json_fail_return("缺少代金券id");
        }
            //查询可兑换代金券模板信息
        $template_info = $voucher->getCanChangeTemplateInfo($vid,intval($this->user_id),0);
        if ($template_info['state'] == false){
            $message = $template_info['msg'];
            return json_fail_return($message);
        }
        $data = $voucher->getCanChangeTemplateInfo($vid,intval($this->user_id),0);
        if ($data['state'] == false){
            return json_fail_return($data['msg']);
        }
        //添加代金券信息
        $data = $voucher->exchangeVoucher($data['info'],$this->user_id,$this->user_info['member_name']);
        if ($data['state'] == true){
            return json_success_return([],$data['msg']);
        } else {
            return json_fail_return($data['msg']);
        }
    }

}